from sqlalchemy import Column, DateTime, Enum, ForeignKey, Integer, String, func
from sqlalchemy.orm import relationship

from app.bot.services.date_time import get_date_time
from app.database.models.base import Base
from app.database.models.m2ms import orders_proxies, proxies_users


class Proxy(Base):
    __tablename__ = "proxy"

    id = Column(Integer, primary_key=True)
    port_https = Column(Integer, nullable=False, unique=False)
    login = Column(String(50), nullable=False, unique=True)
    password = Column(String(50), nullable=False, unique=True)
    server_id = Column(Integer, ForeignKey("server.id"))
    server = relationship("Server", back_populates="proxies")
    users = relationship("User", secondary=proxies_users, back_populates="proxies")
    status = Column(Enum("available", "sold", "invalid"), server_default="available")
    authorization_type = Column(
        Enum("by_ip_address", "by_password"), server_default="by_password"
    )
    reload_type = Column(Enum("by_time", "by_link"))
    reload_period = Column(Integer, server_default="0")
    reload_link = Column(String(50))
    was_added = Column(DateTime, server_default=func.now())
    user_id = Column(Integer, ForeignKey("user.id"))
    orders = relationship("Order", secondary=orders_proxies, back_populates="proxies")

    def __repr__(self):
        return f"<Proxy №{self.id} {self.port_https}>"

    def __str__(self):
        return self.__repr__()
